<template>
   <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
         <el-form-item label="姓名" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable style="width: 200px"
               @keyup.enter="handleQuery" />
         </el-form-item>
         <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
         </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
         <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
            <!-- v-hasPermi="['system:menu:add']" -->
         </el-col>
         <el-col :span="1.5">
            <el-button type="primary" plain icon="Delete" @click="handleDeleteBatch">删除</el-button>
         </el-col>
         <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-if="refreshTable" v-loading="loading" :data="menuList" row-key="menuId"
         :default-expand-all="isExpandAll" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
         @selection-change="handleSelectionChange">
         <el-table-column type="selection" :selectable="selectable" width="55" />
         <el-table-column prop="name" label="名称" width="150"></el-table-column>
         <el-table-column prop="description" label="描述" show-overflow-tooltip></el-table-column>
         <el-table-column prop="createUser" label="创建人" width="150"></el-table-column>
         <el-table-column label="创建时间" align="center" prop="createTime" width="200">
            <template #default="scope">
               <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
         </el-table-column>
         <el-table-column label="操作" fixed="right" align="center" width="210" class-name="small-padding fixed-width">
            <template #default="scope">
               <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:dept:edit']">修改</el-button>
               <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)" v-hasPermi="['system:dept:add']">新增</el-button>
               <el-button v-if="scope.row.parentId != 0" link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:dept:remove']">删除</el-button> -->
               <el-button link type="primary" icon="View" @click="handleView(scope.row)">查看</el-button>
               <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
               <el-button v-if="scope.row.parentId != 0" link type="primary" icon="Delete"
                  @click="handleDelete(scope.row)">删除</el-button>
            </template>
         </el-table-column>
      </el-table>
      <pagination
         v-show="total > 0"
         :total="total"
         v-model:page="queryParams.pageNum"
         v-model:limit="queryParams.pageSize"
         @pagination="getList"
      />
      <!-- 添加或修改系统对话框 -->
      <el-dialog :title="title" v-model="open" width="680px" append-to-body>
         <el-form ref="editRef" :model="form" :rules="rules" label-width="100px">
            <el-row>
               <el-col :span="12">
                  <el-form-item label="系统名称" prop="name">
                     <el-input v-model="form.name" placeholder="请输入系统名称" />
                  </el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="系统描述" prop="description">
                     <el-input type="textarea" resize="none" rows="4" v-model="form.description" placeholder="请输入系统描述" />
                  </el-form-item>
               </el-col>
            </el-row>
         </el-form>
         <template #footer>
            <div class="dialog-footer">
               <el-button type="primary" @click="submitForm">确 定</el-button>
               <el-button @click="cancel">取 消</el-button>
            </div>
         </template>
      </el-dialog>

      <!-- 查看详情对话框 -->
      <el-dialog title="查看详情" v-model="openDetail" width="680px" append-to-body>
         <el-form ref="editRef" label-width="100px">
            <el-row>
               <el-col :span="24">
                  <el-form-item label="系统名称" prop="menuName">
                     <span>{{ detail.name || '-'}}</span>
                  </el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="系统描述" prop="menuType">
                     <span>{{ detail.description || '-'}}</span>
                  </el-form-item>
               </el-col>
            </el-row>
            <el-row>
               <el-col :span="12">
                  <el-form-item label="创建人" prop="menuName">
                     <span>{{ detail.createUser || '-'}}</span>
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="创建时间" prop="menuType">
                     <span>{{ detail.createTime ?  parseTime(detail.createTime) : '-'}}</span>
                  </el-form-item>
               </el-col>
            </el-row>
            <el-row>
               <el-col :span="12">
                  <el-form-item label="修改人" prop="menuName">
                     <span>{{ detail.updateUser || '-'}}</span>
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="修改时间" prop="menuType">
                     <span>{{ detail.updateTime ? parseTime(detail.updateTime) : '-'}}</span>
                  </el-form-item>
               </el-col>
            </el-row>
         </el-form>
         <template #footer>
            <div class="dialog-footer">
               <el-button type="primary" @click="cancelDetail">确 定</el-button>
               <el-button @click="cancelDetail">取 消</el-button>
            </div>
         </template>
      </el-dialog>
   </div>
</template>

<script setup name="Menu">
import { ref } from "vue";
import {
   dataSystemUpdate,
   dataSystemAdd,
   dataSystemGetListByPage,
   dataSystemGetDetail,
   dataSystemDelete
} from "@/api/dataSystem";
import { missionGetList } from "@/api/mission";
import { pullGetList } from "@/api/pull";

const { proxy } = getCurrentInstance();
// const { sys_show_hide, sys_normal_disable } = proxy.useDict("sys_show_hide", "sys_normal_disable");

const menuList = ref([]);
const open = ref(false);
const openDetail = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const title = ref("");
const isExpandAll = ref(false);
const refreshTable = ref(true);
const total = ref(0);

const data = reactive({
   form: {},
   queryParams: {
      pageNum: 1,
      pageSize: 10,
      name: undefined,
   },
   detail: {},
   rules: {
      name: [{ required: true, message: "名称不能为空", trigger: "blur" }]
   }
});

const { queryParams, form, detail, rules } = toRefs(data);
/** 查询系统列表 */
function getList() {
   loading.value = true;
   dataSystemGetListByPage(queryParams.value).then(response => {
      menuList.value = response.data.records;
      total.value = response.data.total;
      loading.value = false;
   }).catch(()=>{
      proxy.$modal.msgError("获取列表数据失败");
      loading.value = false;
   });
}

/** 取消按钮 */
function cancel() {
   open.value = false;
   reset();
}

/** 表单重置 */
function reset() {
   form.value = {};
   detail.value = {};
   editTargetId.value = null;
   proxy.resetForm("editRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
   reset();
   open.value = true;
   title.value = "新增系统";
}

const editTargetId = ref(null)
/** 修改按钮操作 */
async function handleUpdate(row) {
   reset();
   editTargetId.value = row.id
   dataSystemGetDetail({id: row.id}).then(response => {
      form.value = response.data;
      open.value = true;
      title.value = "修改系统";
   });
}


/** 详情显示 */
async function handleView(row) {
   openDetail.value = true
   dataSystemGetDetail({id: row.id}).then(response => {
      detail.value = response.data;
      openDetail.value = true;
      title.value = "修改系统";
   });
}

/** 详情关闭 */
function cancelDetail() {
   openDetail.value = false;
   reset();
}

/** 提交按钮 */
function submitForm() {
   proxy.$refs["editRef"].validate(valid => {
      if (valid) {
         if (editTargetId.value) {
            dataSystemUpdate({...form.value, id: editTargetId.value}).then(response => {
               proxy.$modal.msgSuccess("修改成功");
               open.value = false;
               editTargetId.value = null;
               getList();
            });
            } else {
            dataSystemAdd(form.value).then(response => {
               proxy.$modal.msgSuccess("新增成功");
               open.value = false;
               getList();
            });
         }
      }
   });
}

/** 删除按钮操作 */
function handleDelete(row) {
   proxy.$modal.confirm('是否确认删除名称为"' + row.name + '"的数据项?').then(function () {
      return dataSystemDelete({ ids: row.id });
   }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
   }).catch(() => { });
}

const selectedList = ref([])
function handleSelectionChange(list) {
   selectedList.value = list
}

/** 多选删除操作 */
function handleDeleteBatch() {
   if (selectedList.value.length) {
      proxy.$modal.confirm('是否确认删除选中的' + selectedList.value.length + '个数据?').then(function () {
         const ids = selectedList.value.map(item => item.id).join(',');
         return dataSystemDelete({ ids });
      }).then(() => {
         getList();
         proxy.$modal.msgSuccess("删除成功");
      }).catch(() => { });
   } else {
      proxy.$modal.msgError("请选择要删除的数据");
   }
}

getList();
</script>
